<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <!-- 导入样式表 -->
    <link rel="stylesheet" href="./assets/news.css" />
  </head>

  <body>
    <!-- 新闻列表 -->
    <div id="news-list">
      <!-- 新闻的 item 项 -->
      <div class="news-item">
        <img
          class="thumb"
          src="http://ajax-api.itheima.net:80/public/images/0.webp"
          alt=""
        />
        <div class="right-box">
          <!-- 新闻标题 -->
          <h1 class="title">5G商用在即，三大运营商营收持续下降</h1>
          <div class="footer">
            <div>
              <!-- 新闻来源 -->
              <span>胡润百富</span>&nbsp;&nbsp;
              <!-- 发布日期 -->
              <span>2019-10-28 10:14:38</span>
            </div>
            <!-- 评论数量 -->
            <span>评论数：66</span>
          </div>
        </div>
      </div>
    </div>

    <script src="./lib/axios.js"></script>

    <script>
      const list = document.querySelector('#news-list');

      // 封装渲染函数
      const resderList = function (){
        axios({
          url:'http://ajax-api.itheima.net/api/news',
          method:'GET',
          
        }).then(({data:res})=>{
          const {data} = res
          console.log(data);
          // console.log(cmtcount);
          
          list.innerHTML = data.map(item=>{
            // 每次遍历数组时都会结构每一个对象
            const {cmtcount,img,source,time,title} = item
            return `
            <div class="news-item">
        <img
          class="thumb"
          src="${img}"
          alt=""
        />
        <div class="right-box">
          <h1 class="title">${title}</h1>
          <div class="footer">
            <div>
              <span>${source}</span>&nbsp;&nbsp;
              <span>${time}</span>
            </div>
            <span>评论数：${cmtcount}</span>
          </div>
        </div>
      </div>
            `
          }).join('')
        })


      }

      resderList()


    </script>
     
      
  </body>
</html>
